﻿using pdfer.FileObjects;
using pdfer.Syntax;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace pdfer.PdfObjecs.Fonts
{
    internal class FontDescriptorDictionary : PdfFileObject
    {
        public FontDescriptorDictionary(PdfFileObject pdfFileObject)
            : base(pdfFileObject)
        {
            if (pdfFileObject.TryGetValue(PdfName.Key_Type, out object? val))
            {
                Type = PdfParserUtils.ParseString(val);
                if (Type != PdfTypeConst.Type_FontDescriptor)
                {
                    throw new Exception("类型必须是 /FontDescriptor");
                }
            }
        }
        public string Type { get; set; }

        public string FontName { get { return GetDicStringVal(PdfName.Key_FontName); } }
        public string FontFamily { get { return GetDicStringVal(PdfName.Key_FontFamily); } }
        public string FontStretch { get { return GetDicStringVal(PdfName.Key_FontStretch); } }
        public int StemV { get { return GetDicInt32Val(PdfName.Key_StemV); } }
        public int Ascent { get { return GetDicInt32Val(PdfName.Key_Ascent); } }
        public int Flags { get { return GetDicInt32Val(PdfName.Key_Flags); } }
        public int FontWeight { get { return GetDicInt32Val(PdfName.Key_FontWeight); } }
        public int XHeight { get { return GetDicInt32Val(PdfName.Key_XHeight); } }
        public int Descent { get { return GetDicInt32Val(PdfName.Key_Descent); } }
        public int ItalicAngle { get { return GetDicInt32Val(PdfName.Key_ItalicAngle); } }
        public int CapHeight { get { return GetDicInt32Val(PdfName.Key_CapHeight); } }
        /// <summary>
        /// Type1C          Type1|MMType1 
        /// CIDFontType0C   CIDFontType0
        /// OpenType        TrueType|CIDFontType2 |CIDFontType0|Type1
        /// </summary>
        public PdfFileObject FontFile3
        {
            get
            {
                var id = GetDicStringVal(PdfName.Key_FontFile3);
                if (id == null || id[^1] != 'R')
                {
                    return null;
                }
                var obj = GetIndirectObj(id);
                return obj;
            }
        }
        public PdfFileObject CIDSet
        {
            get
            {
                var id = GetDicStringVal(PdfName.Key_CIDSet);
                if (id == null || id[^1] != 'R')
                {
                    return null;
                }
                var obj = GetIndirectObj(id);
                return obj;
            }
        }
        private Vector4? _FontBBox;

        /// <summary>
        /// 矩形
        /// </summary>
        public Vector4 FontBBox
        {
            get
            {
                if (_FontBBox == null)
                {
                    var _tmp = GetDicListVal(PdfName.Key_FontBBox, PdfParserUtils.Trans2Single);
                    _FontBBox = new Vector4(_tmp.ToArray());
                }
                return _FontBBox ?? default;
            }
        }
        ///FontName /AdobeKaitiStd-Regular
        ///StemV 76
        ///Ascent 987
        ///Flags 6
        ///FontWeight 400
        ///FontFamily (asdasd)
        ///XHeight 501
        ///FontStretch /Normal
        ///Descent -270
        ///ItalicAngle 0
        ///CIDSet 37 0 R
        ///FontBBox[-148 - 270 1038 987]
        ///Type /FontDescriptor
        ///CapHeight 740
    }
}
